<?php
namespace Info\Controller;

use Common\Controller\BehaviorController;
use Common\Model\InfoContentModel;
use Common\Model\InfoShareModel;
class PostController extends BehaviorController
{

    public function index()
    {
        $cid = I('cid');
        if ($cid) {
            $temp = explode('_', $cid);
            $map['id'] = array('in', $temp);
            $category = M('info_category')->field("category")->where($map)->select();
            if ($category) {
                $category = implode('/', array_column($category, 'category'));
                $area = C("city_area");
                $this->assign('cid', $cid)->assign('category', $category)->assign("area", $area);
                $this->display(':post');
                exit();
            }
        }

        $category = M('info_category')->field("id,pid,directory,category")->select();
        $cateData = formateCategory($category);
        $this->assign('category', $cateData);
        $this->display(':post_category');

    }

    public function add()
    {
        if (IS_POST) {
            //验证码判断
            $vali = $this->CheckCode(I("validate"));
            if ($vali) {
                $info = new InfoContentModel();
                $return = $info->addInfoContent();
                if (!$return) {
                    $this->error($info->getError());
                    return false;
                }
                $shareModel = new InfoShareModel();
                $sid = $shareModel->addRecord($return);
                $this->redirect('Info/post/addSuccess', array('id' => $return, 'sid' => $sid));
            } else {
                $this->error("验证码输入错误");
            }
        }
    }



    public function addSuccess()
    {
        $id = I('id');
        $sid = I("sid");
        if ($id && $sid) {
            $info = new InfoContentModel();
            $data = $info->find($id);
            if ($data) {
                $this->assign('data', $data)->assign("sid", $sid);
                $this->display(':post_success');
                exit();
            }
        }
        $this->redirect('/info/info/error404', array('id' => $id, 'sid' => $sid));
    }

    public function thumInfoImg($imgPath = '', $w = 800, $h = 800 , $savePath = null,$isWater = true)
    {
        if ($imgPath != '') {
            $savePath = $savePath == null ? C('thumb_save_path') : $savePath;
            $image = new \Think\Image();
            $image->open($imgPath);
            $width = $image->width(); // 返回图片的宽度
            $height = $image->height(); // 返回图片的高度
            if($height < 200 || $width < 200 || $w < 200 || $h < 200) $isWater = false;
            $fileName = basename($imgPath);
            $savePath = C('thumb_save_path')."{$w}_{$h}/{$fileName}";
            $dir = dirname($savePath);
            if (!is_dir($dir)) {
                mkdir($dir, 0777, true);
            }
            if ($width > $w || $height > $h) {
                if($isWater){
                    $image->crop($w, $h)->water(C('thumb_water'))->save($savePath);
                }else{
                    $image->crop($w, $h)->save($savePath);
                }
            } else {
                if($isWater){
                    $image->water(C('thumb_water'))->save($savePath);
                }else{
                    copy($imgPath, $savePath);
                }

            }
            return $savePath;
        }else{
            return null;
        }
    }

    public function uploadImg()
    {
        if ($_FILES) {
            if ($_FILES['uploadfile']['error'] != 4) {
                //上传图像
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728;// 设置附件上传大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = './UploadFiles/'; // 设置附件上传根目录
                $upload->savePath = 'info/temp/'; // 设置附件上传（子）目录
                $upload->saveName = '58shiyan_' . date('ymdHis');
                $upload->autoSub = false;
                $file = $upload->upload();

                if (!$file) {// 上传错误提示错误信息
                    $this->error = '上传错误';
                    //return false;
                    exit('');
                } else {
                    $tempPath = './UploadFiles/' . $file['uploadfile']['savepath'] . $file['uploadfile']['savename'];
                    $thumSavePath = $this->thumInfoImg($tempPath, 600, 600);
                    //$thumSavePath = $this->thumInfoImg($tempPath, 170, 100);
                    $path = substr($thumSavePath, 1);
                    echo $path;
                    exit();
                }

            }
        }

    }

    public function CheckCode($code)
    {
        $verify = new \Org\Util\Verify();
        $bool = $verify->check($code);
        if ($bool) {
            return true;
        } else {
            return false;
        }
    }

    public function CheckValidateCode()
    {
        $code = I("code");
        $verify = new \Org\Util\Verify();
        $bool = $verify->check($code);
        if ($bool) {
            echo 'True';
            die;
        } else {
            echo 'false';
            die;
        }
    }

    public function validateCode()
    {
        //实例化验证码
        $Verify = new \Think\Verify();
        $Verify->fontSize = 16;//字体大小
        $Verify->length = 4;//长度
        $Verify->useNoise = true;//关闭杂点
        $Verify->useCurve = false;
        $Verify->useImgBg = false;//是否使用背景图片
        $Verify->imageW = 108;//长
        $Verify->imageH = 30;//宽
        $Verify->bg = array(236, 236, 236);//背景颜色
        $Verify->entry();
    }
}